<template>
  <div class="products">
    <div class="page-header">
      <h1>产品管理</h1>
      <el-button type="primary" @click="showAddDialog = true" :icon="Plus">
        添加产品
      </el-button>
    </div>

    <el-table :data="products" v-loading="loading">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="name" label="产品名称" />
      <el-table-column prop="price" label="价格" width="120">
        <template #default="{ row }">
          ¥{{ row.price }}
        </template>
      </el-table-column>
      <el-table-column prop="category" label="分类" width="120" />
      <el-table-column prop="stock" label="库存" width="100" />
      <el-table-column prop="description" label="描述" />
      <el-table-column label="操作" width="120">
        <template #default>
          <el-button type="primary" link>编辑</el-button>
          <el-button type="danger" link>删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加产品对话框 -->
    <el-dialog v-model="showAddDialog" title="添加产品" width="500px">
      <el-form :model="newProduct" :rules="rules" ref="productFormRef">
        <el-form-item label="产品名称" prop="name">
          <el-input v-model="newProduct.name" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input-number v-model="newProduct.price" :min="0" :precision="2" style="width: 100%" />
        </el-form-item>
        <el-form-item label="分类" prop="category">
          <el-select v-model="newProduct.category" placeholder="请选择分类" style="width: 100%">
            <el-option label="电脑" value="电脑" />
            <el-option label="手机" value="手机" />
            <el-option label="平板" value="平板" />
            <el-option label="配件" value="配件" />
            <el-option label="穿戴" value="穿戴" />
          </el-select>
        </el-form-item>
        <el-form-item label="库存" prop="stock">
          <el-input-number v-model="newProduct.stock" :min="0" style="width: 100%" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="newProduct.description" type="textarea" :rows="3" />
        </el-form-item>
      </el-form>
      
      <template #footer>
        <el-button @click="showAddDialog = false">取消</el-button>
        <el-button type="primary" @click="handleAddProduct" :loading="adding">
          确认添加
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { productAPI } from '@/api'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'

const products = ref([])
const loading = ref(false)
const showAddDialog = ref(false)
const adding = ref(false)
const productFormRef = ref()

const newProduct = ref({
  name: '',
  price: 0,
  category: '',
  stock: 0,
  description: ''
})

const rules = {
  name: [{ required: true, message: '请输入产品名称', trigger: 'blur' }],
  price: [{ required: true, message: '请输入价格', trigger: 'blur' }],
  category: [{ required: true, message: '请选择分类', trigger: 'change' }]
}

const loadProducts = async () => {
  loading.value = true
  try {
    const response = await productAPI.getProducts()
    products.value = response.data
  } catch (error) {
    ElMessage.error('加载产品列表失败')
  } finally {
    loading.value = false
  }
}

const handleAddProduct = async () => {
  if (!productFormRef.value) return
  
  try {
    const valid = await productFormRef.value.validate()
    if (valid) {
      adding.value = true
      await productAPI.addProduct(newProduct.value)
      ElMessage.success('添加产品成功')
      showAddDialog.value = false
      loadProducts()
      // 重置表单
      newProduct.value = {
        name: '',
        price: 0,
        category: '',
        stock: 0,
        description: ''
      }
    }
  } catch (error) {
    ElMessage.error('添加产品失败')
  } finally {
    adding.value = false
  }
}

onMounted(() => {
  loadProducts()
})
</script>

<style scoped>
.products {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
</style>